/*
 * jxSwing Project (2014)
 * 
 * by Denilson Edinaldo Pinto
 * 
 * LGPL
 * Copyright (C) 2014
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.jxSwing.component.dialog;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableModel;

import org.jxSwing.border.TitleBorderFactory;
import org.jxSwing.component.button.ClearButton;
import org.jxSwing.component.button.FindButton;
import org.jxSwing.component.panel.BeanTablePanel;
import org.jxSwing.component.table.BeanTableColumn;
import org.jxSwing.i18n.I18N;

/**
 * @author Denilson Edinaldo Pinto
 * @porject jxSwing
 * @package org.jxSwing.component.dialog
 * @date 22/03/2014 - 18:15:19
 * @param <Bean>
 */
public class TableFilterDialog<Bean> extends Dialog {
	
	private static final long serialVersionUID = -3446895322552682476L;
	
	private JLabel lblSearch;
	
	private JTextField txtSearch;
	
	private JCheckBox chkCaseSensitive;
	
	private JCheckBox chkSelectAll;
	
	private JPanel pnlColumns;
	
	private JScrollPane scrlpnColumns;
	
	private JTable tblColumns;
	
	private ClearButton btnClean;
	
	private FindButton btnFilter;
	
	private final BeanTablePanel<Bean> pnlBeanTable;
	
	/**
	 * Create the dialog.
	 * 
	 * @param owner
	 * @param pnlBeanTable
	 */
	public TableFilterDialog(final Frame owner, final BeanTablePanel<Bean> pnlBeanTable) {
	
		super(owner, true);
		
		this.pnlBeanTable = pnlBeanTable;
		
		initGUI();
	}
	
	/**
	 * 
	 */
	private void applyFilter() {
	
		final List<Integer> columnsList = new ArrayList<>();
		
		//
		for(int index = 0; index < getTblColumns().getRowCount(); index++) {
			
			//
			if((Boolean)getTblColumns().getValueAt(index, 1)) {
				
				columnsList.add(index);
			}
		}
		
		final int[] columns = new int[columnsList.size()];
		
		//
		for(int index = 0; index < columnsList.size(); index++) {
			
			columns[index] = columnsList.get(index);
		}
		
		//
		getPnlBeanTable().filter(getTxtSearch().getText(), getChkCaseSensitive().isSelected(), columns);
		
		dispose();
	}
	
	/**
	 * 
	 */
	private void initGUI() {
	
		getContentPane().setLayout(null);
		getContentPane().add(getLblSearch());
		getContentPane().add(getTxtSearch());
		getContentPane().add(getChkCaseSensitive());
		getContentPane().add(getChkSelectAll());
		getContentPane().add(getPnlColumns());
		getContentPane().add(getBtnClean());
		getContentPane().add(getBtnFilter());
		
		//
		if(getOwner() != null && !getOwner().getIconImages().isEmpty()) {
			
			setIconImage(getOwner().getIconImages().get(0));
			
		}else {
			
			setIconImage(getPnlBeanTable().getOwnerWindowIcon());
		}
		
		setTitle(I18N.component().search());
		setBounds(100, 100, 495, 270);
		setResizable(false);
		centralize();
	}
	
	/**
	 * @return the btnClean
	 */
	public ClearButton getBtnClean() {
	
		if(btnClean == null) {
			btnClean = new ClearButton(I18N.component().clean());
			btnClean.setBounds(210, 205, 130, 30);
			btnClean.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(final ActionEvent actionEvent) {
				
					getTxtSearch().setText("");
					getChkCaseSensitive().setSelected(false);
					getChkSelectAll().setSelected(false);
					
					applyFilter();
					
					getPnlBeanTable().getHeaderBar().setVisibleClearFilter(false);
				}
			});
		}
		return btnClean;
	}
	
	/**
	 * @return the btnFilter
	 */
	public FindButton getBtnFilter() {
	
		if(btnFilter == null) {
			btnFilter = new FindButton(I18N.component().filter());
			btnFilter.setToolTipText(I18N.component().filter());
			btnFilter.setBounds(350, 205, 130, 30);
			btnFilter.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(final ActionEvent actionEvent) {
				
					applyFilter();
				}
			});
		}
		return btnFilter;
	}
	
	/**
	 * @return the chkCaseSensitive
	 */
	public JCheckBox getChkCaseSensitive() {
	
		if(chkCaseSensitive == null) {
			chkCaseSensitive = new JCheckBox(I18N.component().caseSensitive());
			chkCaseSensitive.setHorizontalAlignment(SwingConstants.TRAILING);
			chkCaseSensitive.setBounds(10, 40, 310, 25);
		}
		return chkCaseSensitive;
	}
	
	/**
	 * @return the chkSelectAll
	 */
	public JCheckBox getChkSelectAll() {
	
		if(chkSelectAll == null) {
			chkSelectAll = new JCheckBox(I18N.component().selectAll1());
			chkSelectAll.setHorizontalAlignment(SwingConstants.TRAILING);
			chkSelectAll.setBounds(330, 40, 150, 25);
			chkSelectAll.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(final ActionEvent actionEvent) {
				
					//
					for(int index = 0; index < getTblColumns().getRowCount(); index++) {
						
						getTblColumns().setValueAt(chkSelectAll.isSelected(), index, 1);
					}
				}
			});
		}
		return chkSelectAll;
	}
	
	/**
	 * @return the lblSearch
	 */
	public JLabel getLblSearch() {
	
		if(lblSearch == null) {
			lblSearch = new JLabel(I18N.component().search() + ":");
			lblSearch.setBounds(10, 10, 100, 25);
		}
		return lblSearch;
	}
	
	/**
	 * @return the pnlBeanTable
	 */
	public BeanTablePanel<Bean> getPnlBeanTable() {
	
		return pnlBeanTable;
	}
	
	/**
	 * @return the pnlColumns
	 */
	public JPanel getPnlColumns() {
	
		if(pnlColumns == null) {
			pnlColumns = new JPanel();
			pnlColumns.setBounds(10, 70, 470, 130);
			pnlColumns.setBorder(TitleBorderFactory.createBorder(I18N.component().column() + ":"));
			pnlColumns.setLayout(new BorderLayout(0, 0));
			pnlColumns.add(getScrlpnColumns(), BorderLayout.CENTER);
		}
		return pnlColumns;
	}
	
	/**
	 * @return the scrlpnColumns
	 */
	public JScrollPane getScrlpnColumns() {
	
		if(scrlpnColumns == null) {
			scrlpnColumns = new JScrollPane();
			scrlpnColumns.setViewportView(getTblColumns());
		}
		return scrlpnColumns;
	}
	
	/**
	 * @return the tblColumns
	 */
	public JTable getTblColumns() {
	
		if(tblColumns == null) {
			
			final DefaultTableModel model = new DefaultTableModel(null, new String[]{I18N.component().column(), I18N.component().search()}) {
				
				private static final long serialVersionUID = 5702593870936373139L;
				
				boolean[] columnEditables = new boolean[]{false, true};
				
				Class<?>[] columnTypes = new Class[]{String.class, Boolean.class};
				
				@Override
				public Class<?> getColumnClass(final int columnIndex) {
				
					return columnTypes[columnIndex];
				}
				
				@Override
				public boolean isCellEditable(final int row, final int column) {
				
					return columnEditables[column];
				}
			};
			
			//
			for(final BeanTableColumn column : getPnlBeanTable().getColumns()) {
				
				model.addRow(new Object[]{column.getAlias(), false});
			}
			
			tblColumns = new JTable(model);
			tblColumns.getColumnModel().getColumn(0).setPreferredWidth(350);
			tblColumns.getColumnModel().getColumn(0).setMinWidth(150);
			tblColumns.getColumnModel().getColumn(1).setMinWidth(75);
		}
		return tblColumns;
	}
	
	/**
	 * @return the txtSearch
	 */
	public JTextField getTxtSearch() {
	
		if(txtSearch == null) {
			txtSearch = new JTextField();
			txtSearch.setMaximumSize(new Dimension(300, 25));
			txtSearch.setBounds(115, 10, 365, 25);
			txtSearch.addKeyListener(new KeyAdapter() {
				
				@Override
				public void keyPressed(final KeyEvent keyEvent) {
				
					//
					if(keyEvent.getKeyCode() == KeyEvent.VK_ENTER) {
						
						applyFilter();
					}
				}
			});
		}
		return txtSearch;
	}
}
